package com.car.advertising.activity.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.car.advertising.R;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.bean.PersonalBean;
import com.car.advertising.listener.SaveEditListener;
import com.car.advertising.view.CircleImageViewWithStroke;
import com.car.advertising.view.EditTextDeleteView;
import com.support.widget.layout.adapter.BaseRecyclerViewAdapter;
import com.support.widget.layout.holder.BaseViewHolder;

import java.util.List;

/**
 * 开发者：LuoYi
 * Time: 2017 16:09 2017/4/22 04
 */

public class PersonalInfoAdapter extends BaseRecyclerViewAdapter<PersonalBean, BaseViewHolder> {

    public PersonalCommitListener commitListener;
    private static int PERSONAL_UPDATE = 0;
    private static int PERSONAL_SHOW = 1;
    private String imgHttp = HttpUrl.getInstance().URL_041;
    private int accountIcon = R.mipmap.ic_launcher;

    public PersonalInfoAdapter(Context context, List<PersonalBean> strings) {
        super(context, strings);
    }

    public void setCommitListener(PersonalCommitListener commitListener) {
        this.commitListener = commitListener;
    }

    public void refreshList(List<PersonalBean> strings) {
        this.mDatas = strings;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas.get(position).personalId == 7) {
            return PERSONAL_UPDATE;
        } else {
            return PERSONAL_SHOW;
        }
    }

    @Override
    protected void bindView(BaseViewHolder holder, int position, PersonalBean model) {
        if (holder instanceof PersonalHolder) {
            setPersonalText((PersonalHolder) holder, position, model);
        } else if (holder instanceof PersonalCommitHolder) {
            if (model.personalUpdate) {
                ((PersonalCommitHolder) holder).commitTv.setVisibility(View.VISIBLE);
            } else {
                ((PersonalCommitHolder) holder).commitTv.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == PERSONAL_SHOW) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_personal_information, parent, false);
            return new PersonalHolder(mContext, view, this);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_update_commit, parent, false);
            return new PersonalCommitHolder(mContext, view, this);
        }
    }

    private void setPersonalText(PersonalHolder personalHolder, int position, PersonalBean model) {

        personalHolder.userNameTv.setText(model.getPersonalName());
        if (model.isPersonalUpdate()) {
            if (position == 0) {
                personalHolder.personalJumpIv.setVisibility(View.VISIBLE);
                personalHolder.headIconCiv.setVisibility(View.VISIBLE);
                personalHolder.userNameContentTv.setVisibility(View.GONE);
                personalHolder.userNameUpdateEtv.setVisibility(View.GONE);
                Glide.with(mContext).load(imgHttp + model.getPersonalHeadImg()).placeholder(accountIcon).error(accountIcon).into(personalHolder.headIconCiv);//
            } else {
                personalHolder.personalJumpIv.setVisibility(View.GONE);
                personalHolder.headIconCiv.setVisibility(View.GONE);
                personalHolder.userNameContentTv.setVisibility(View.GONE);
                personalHolder.userNameUpdateEtv.setVisibility(View.VISIBLE);
                personalHolder.userNameUpdateEtv.setText(setCarAuthentication(personalHolder.userNameUpdateEtv, model.getPersonalContentHint(), model.getPersonalNameContent()));
            }
            personalHolder.userNameUpdateEtv.addTextChangedListener(new PersonalUpdateTextWatcher(personalHolder.userNameUpdateEtv));
            //通过设置tag，防止position紊乱
            personalHolder.userNameUpdateEtv.setTag(position);
        } else {
            if (position == 0) {
                personalHolder.personalJumpIv.setVisibility(View.GONE);
                personalHolder.headIconCiv.setVisibility(View.VISIBLE);
                personalHolder.userNameContentTv.setVisibility(View.GONE);
                personalHolder.userNameUpdateEtv.setVisibility(View.GONE);
                Glide.with(mContext).load(imgHttp + model.getPersonalHeadImg()).placeholder(accountIcon).error(accountIcon).into(personalHolder.headIconCiv);//
            } else {
                personalHolder.personalJumpIv.setVisibility(View.GONE);
                personalHolder.headIconCiv.setVisibility(View.GONE);
                personalHolder.userNameContentTv.setVisibility(View.VISIBLE);
                personalHolder.userNameUpdateEtv.setVisibility(View.GONE);
                personalHolder.userNameContentTv.setText(setCarAuthentication(personalHolder.userNameUpdateEtv, model.getPersonalContentHint(), model.getPersonalNameContent()));
            }
        }
    }

    private String setCarAuthentication(EditTextDeleteView userNameUpdateEtv, String contentHint, String carAuthentication) {
        userNameUpdateEtv.setHint(contentHint);
        if (TextUtils.isEmpty(carAuthentication)) {
            return "";
        }
        switch (carAuthentication) { // 是否车主认证0：未认证1：审核中2：审核通过3：审核不通过
            case "0":
                return "未认证";
            case "1":
                return "审核中";
            case "2":
                return "审核通过";
            case "3":
                return "审核不通过";
        }
        return carAuthentication;
    }

    class PersonalHolder extends BaseViewHolder {

        public TextView userNameTv;
        public TextView userNameContentTv;
        public EditTextDeleteView userNameUpdateEtv;
        public CircleImageViewWithStroke headIconCiv;
        public ImageView personalJumpIv;

        public PersonalHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            userNameTv = (TextView) rootView.findViewById(R.id.personal_user_name_tv);
            userNameContentTv = (TextView) rootView.findViewById(R.id.personal_user_name_content_tv);
            userNameUpdateEtv = (EditTextDeleteView) rootView.findViewById(R.id.personal_user_name_update_etv);
            headIconCiv = (CircleImageViewWithStroke) rootView.findViewById(R.id.personal_head_icon_civ);
            personalJumpIv = (ImageView) rootView.findViewById(R.id.personal_jump_iv);
            headIconCiv.setOnClickListener(headOnclick);
        }

        View.OnClickListener headOnclick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                commitListener.onHeadIcon();
            }
        };
    }

    class PersonalCommitHolder extends BaseViewHolder {

        public TextView commitTv;


        public PersonalCommitHolder(Context context, View rootView, BaseRecyclerViewAdapter mBaseRecyclerViewAdapter) {
            super(context, rootView, mBaseRecyclerViewAdapter);
            commitTv = (TextView) rootView.findViewById(R.id.update_commit_tv);
            commitTv.setOnClickListener(clickListener);
        }

        View.OnClickListener clickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                commitListener.onPersonalCommit(6);
            }
        };
    }

    class PersonalUpdateTextWatcher implements TextWatcher {

        private EditText editText;

        public PersonalUpdateTextWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            //用户输入完毕后，处理输入数据，回调给主界面处理
            SaveEditListener listener = (SaveEditListener) mContext;
            if (s != null) {
                listener.getEditPositionText(Integer.parseInt(editText.getTag().toString()), s.toString());
            }
        }
    }

    public interface PersonalCommitListener {

        void onHeadIcon();

        void onPersonalCommit(int position);

    }
}
